/*
    Copyright (C) 2008-2009 Dmitry Gusev <dmitry.gusev@gmail.com>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

 */

package dmitrygusev.web4konverta.mobile;

import java.io.IOException;

import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;

/**
 *
 * @author dmitrygusev
 */
public class VersionChecker {

    private String checkVersionURL;

    private String currentVersionString;

    public VersionChecker(String checkVersionURL, String currentVersionString) {
        this.checkVersionURL = checkVersionURL;

        this.currentVersionString = currentVersionString;
    }

    public String getCurrentVersion() {
        return currentVersionString;
    }

    public String getLatestVersionString() {
        String newVersionString = readLatestVersionString();

        if (newVersionString == null) {
            return currentVersionString;
        }

        if (olderThan(currentVersionString, newVersionString)) {
            return newVersionString;
        }

        return currentVersionString;
    }

    public static int getMajorVersion(String versionString) {
        String[] parts = Utils.splitString(versionString, ".");
        return Integer.valueOf(parts[0]).intValue();
    }

    public static int getMinorVersion(String versionString) {
        String[] parts = Utils.splitString(versionString, ".");
        return Integer.valueOf(parts[1]).intValue();
    }

    public static int getBuildNumber(String versionString) {
        String[] parts = Utils.splitString(versionString, ".");
        return Integer.valueOf(parts[2]).intValue();
    }

    public static boolean olderThan(String versionA, String versionB) {
        return getMajorVersion(versionA) < getMajorVersion(versionB)
            || (getMajorVersion(versionA) == getMajorVersion(versionB)
                && getMinorVersion(versionA) < getMinorVersion(versionB))
            || (getMajorVersion(versionA) == getMajorVersion(versionB)
                && getMinorVersion(versionA) == getMinorVersion(versionB)
                && getBuildNumber(versionA)  < getBuildNumber(versionB));
    }

    public String readLatestVersionString() {
        HttpConnection conn = null;
        try {
            conn = (HttpConnection) Connector.open(checkVersionURL);

            int rc = conn.getResponseCode();

            if (rc != HttpConnection.HTTP_OK) {
                return null;
            }

            final String html = Utils.readHtml(conn);

            return extractVersionString(html);
        } catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            if (conn != null) {
                try {
                    conn.close();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
                conn = null;
            }
        }
        return null;
    }

    public static String extractVersionString(final String html) {
        String versionPrefix = "Latest Version: ";

        int beginIndex = html.indexOf(versionPrefix) + versionPrefix.length();

        if (beginIndex == -1) {
            return null;
        }

        int endIndex = beginIndex;

        char c;

        int dotCounter = 0;

        do {
            endIndex++;
            c = html.charAt(endIndex);

            if (c == '.') {
                dotCounter++;
            }
        } while (Character.isDigit(c) || (c == '.' && dotCounter <= 2));

        return html.substring(beginIndex, endIndex);
    }

}